package com.gzw.dms.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/10/21.
 */
public class SelectUserAdapter extends BaseAdapter {

    private Context mContext;
    private List<UserInfo> lsInfo;
    private static HashMap<Integer, Boolean> isSelected;

    public SelectUserAdapter(Context _mContext,
                          List<UserInfo> _lsInfo) {
        this.mContext = _mContext;
        this.lsInfo = _lsInfo;

        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }



    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lsInfo.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public View getView(final int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        // 加载布局文件
        final  Holder mHolder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_selectuser, null);
            mHolder = new Holder();
            mHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
            mHolder.tv_adress = (TextView) convertView.findViewById(R.id.tv_adress);
            mHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);

            mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);

            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }

        UserInfo shippingOrderInfo = lsInfo.get(arg0);
        mHolder.tv_username.setText("联系人:"+"  "+shippingOrderInfo.getRealName());
        mHolder.tv_phone.setText("电话:"+"  "+shippingOrderInfo.getUserPhone());
        mHolder.tv_adress.setText("地址:"+"  "+shippingOrderInfo.getUser_address());

        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mHolder.cb.setChecked(isChecked);
                isSelected.put(arg0, isChecked);
            }
        });
        mHolder.cb.setChecked(getIsSelected().get(arg0));
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        SelectUserAdapter.isSelected = isSelected;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return lsInfo.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lsInfo.size();
    }

    public static class Holder {
        public TextView tv_username;
        public TextView tv_adress;
        public TextView tv_phone;

        public CheckBox cb;
    }
}

